package com.cz.background.common.util;


import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;



public class EyDataEncryptorHelper {

    private Logger logger = LoggerFactory.getLogger(EyDataEncryptorHelper.class);

   private static final String PUBLIC_KEY ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCEqv2TMxj/EgXmCO9zbyiIA/omXprrrmZhudN237lQ3qWSsxaPZlU0c79PtEEEc8nW/wAPkzUIiuOWX4N9SebZYBh+7+VmeFuyjNvMKOFTxdoQdVlWk4u8k3Jm4OBigH4YgtcSrU+LmZLQ7DD5zp9IBxWpUVewVrYyehfUHx3ppwIDAQAB";

    private static final String RES_IV = "wt#f7iZbHC*j5Z7F";
    private static final String RES_PRIVATE_KEY = "8yoHY1JYh7/D2yTxG27ocw==";

    public static String encrypt(String data) {

        // 创建RSA加密对象
        RSA rsa = new RSA(null, PUBLIC_KEY);

        // 分段加密
        byte[] encryptedData = rsa.encrypt(data.getBytes(StandardCharsets.UTF_8), KeyType.PublicKey);

        // Base64编码
        return Base64.encode(encryptedData);
    }


    public static String decrypt(String data) {
        // 解密
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding,
                RES_PRIVATE_KEY.getBytes(), RES_IV.getBytes());
        String decryptedData = aes.decryptStr(data);
        return decryptedData;
    }


    public static void main(String[] args) {

    }
}
